package messageprocessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Observer;

import messageprocessors.BaseClient;
import messages.DSMessage;
import messages.DSMessageType;




@SuppressWarnings("serial")
public class Appclient extends BaseClient implements Serializable {
Ware aktArt=new Ware();
Map<String, String> receivedMap2 = new HashMap<String, String>();
	/**
 * @return the receivedMap2
 */
public Map<String, String> getReceivedMap2() {
	
		return receivedMap2;

}

/**
 * @param receivedMap2 the receivedMap2 to set
 */
public void setReceivedMap2(Map<String, String> receivedMap2) {
	this.receivedMap2 = receivedMap2;
}

	public Appclient() {
		this("appclient", "appclient");
		
	}

	public Appclient(Observer ob) {
		this("appclient", "appclient", ob);
	}

	public Appclient(String id, String xmppPassword) {
		super(id, xmppPassword);
		// TODO Auto-generated constructor stub
	}

	public Appclient(String id, String xmppPassword, Observer ob) {
		this(id, xmppPassword);
		this.addObserver(ob);
	}

	public void connect()throws Exception{
		super.connect();
	}
	public void disconnect(){
		super.disconnect();
	}

	public void constructmessageRequest(String target) throws Exception {
		DSMessage message = new DSMessage();
		message.setTargetId(target);
		message.setOriginId("appclient");
		message.setType(DSMessageType.STATUSREQUEST);

		// sendMessage(message,target);
		System.out.println(message);

	}

	public void constructmessageCommand(String target, Map<String, String> map)
			throws Exception {

		DSMessage message = new DSMessage(map);
		message.setTargetId(target);
		message.setOriginId("appclient");
		message.setType(DSMessageType.COMMAND);
		// sendMessage(message,target);

	}

	@Override
	public void processMessage(DSMessage message) throws Exception {

		if (message == null) {
			throw new Exception("The received message is null!");
		}

		// Check for errors ---> handle errors
		if (message.getError() != null) {
			this.setLastErrorRetrieved(message.getError());
		}
		// status request message
		if (message.getType() == DSMessageType.STATUSREQUEST) {
			processStatusRequestMessage(message);
			this.setChanged();
			this.notifyObservers(message);
		

		}
		// command message
		else if (message.getType() == DSMessageType.COMMAND) {
			processCommandMessage(message);
			this.setChanged();
			this.notifyObservers(message);
		
			
		
		} 
		// status message
		else if (message.getType() == DSMessageType.STATUS) {
			processStatusMessage(message);
			this.setChanged();
			this.notifyObservers(message);
		
		}
		
		else if (message.getType()==DSMessageType.CODEREQUEST){
			processCodeRequest(message);
			this.setChanged();
			this.notifyObservers(message);
		
			
		}
		this.setLastMessageReceived(message);
		this.setChanged();
		this.notifyObservers(message);
	
		
		
	}
	
	private void processCodeRequest(DSMessage message) {
		Ware neuArt=new Ware();
		Map<String, String> receivedMap = new HashMap<String, String>();
		receivedMap=message.getPropertyMap();
		this.setReceivedMap2(receivedMap);
		this.receivedMap2=receivedMap;
		System.out.println(receivedMap);
		if (receivedMap.containsKey("barcode")){
			neuArt=neuArt.generateWareIU(receivedMap);
		}else if (receivedMap.containsKey("wareneingangsid")){
			neuArt=neuArt.generateWare(receivedMap);
		}
		
		this.aktArt=neuArt;
		//aktArt=neuArt;
		this.setAktArt(neuArt);
		
		//setAktArt(neuArt);
		
		// TODO Auto-generated method stub

	}

	
	private void processStatusMessage(DSMessage message) {
		
	}

	private void processCommandMessage(DSMessage message) {
		Ware neuArt=new Ware();
		//neuArt=neuArt.generateWare(message.getPropertyMap());
		Map<String, String> receivedMap = new HashMap<String, String>();
		receivedMap=message.getPropertyMap();
		this.setReceivedMap2(receivedMap);
		this.receivedMap2=receivedMap;
		this.aktArt=neuArt;
		//aktArt=neuArt;
		this.setAktArt(neuArt);
		
		//setAktArt(neuArt);
		// TODO Auto-generated method stub

	}

	private void processStatusRequestMessage(DSMessage message) {

		// TODO Auto-generated method stub

	}
	/**
	 * @return the aktArt
	 */
	public Ware getAktArt() {
		return aktArt;
	}

	/**
	 * @param aktArt the aktArt to set
	 */
	public void setAktArt(Ware aktArt) {
		this.aktArt = aktArt;
		this.setChanged();
		this.notifyObservers(aktArt);
		//this.notifyAll();
	}

	@Override
	protected Map<String, String> generatePropertyMap() {
		return null;
	}

}
